package com.knowswift.security.account;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 权限类
 * {@code name} 权限名
 * {@code limit} 拥有情况
 * {@code items} 子权限
 *
 * @Date 2021/5/12
 **/

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@NoArgsConstructor
@AllArgsConstructor
public class Authority implements Serializable {
    private String name;
    private String value;
    private Boolean limit; // 是否有权限
    private List<Authority> items;

    public Authority(String name, String value) {
        this.name = name;
        this.value = value;
        this.limit = Boolean.TRUE;
    }

    public Authority(String name, String value, Boolean limit) {
        this.name = name;
        this.value = value;
        this.limit = limit;
        items = new ArrayList<>();
    }

    public Authority add(Authority authority) {
        if (this.items == null) {
            this.items = new ArrayList<>();
        }
        this.items.add(authority);
        return this;
    }

    public Authority add(String name, String value) {
        return add(name, value, true);
    }

    public Authority add(String name, String value, Boolean limit) {
        Authority authority = new Authority(name, value, limit);
        if (this.items == null) {
            this.items = new ArrayList<>();
        }
        this.items.add(authority);
        return this;
    }
}
